package com.taobao.top
{
	import com.adobe.serialization.json.JSON;
	import com.taobao.top.converter.SellerCatConverter;
	import com.taobao.top.events.TOPEvent;
	import com.taobao.top.model.SellerCat;
	import com.taobao.top.resultmodel.SellerCatAddResult;
	import com.taobao.top.resultmodel.SellerCatUpdateResult;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	/**
	 * 卖家自定义类目 
	 * @author Administrator
	 * 
	 */	
	public class TOPSellerCat extends EventDispatcher
	{
		public function TOPSellerCat()
		{
		}
		
		public function getSellerCatsList(nick:String):void
		{
			var topURLLdr:TOPURLLoader = new TOPURLLoader();
			var reqParamArr:Array = new Array();
			reqParamArr=[new TopRequestParam("method","taobao.sellercats.list.get"),new TopRequestParam("nick",nick)];
			topURLLdr.addEventListener(Event.COMPLETE,sellerCatsGetHandler);
			topURLLdr.loadData(reqParamArr);
		}
		
		public function addSellerCatsList(sellerCat:SellerCat):void
		{
			var topURLLdr:TOPURLLoader = new TOPURLLoader();
			var reqParamArr:Array = new Array();
			reqParamArr=[new TopRequestParam("method","taobao.sellercats.list.add"),
			new TopRequestParam("name",sellerCat.name),
			new TopRequestParam("pict_url",sellerCat.pict_url),
			new TopRequestParam("parent_cid",sellerCat.parent_cid),
			new TopRequestParam("sort_order",sellerCat.sort_order.toString())];
			topURLLdr.addEventListener(Event.COMPLETE,sellerCatsAddHandler);
			topURLLdr.loadData(reqParamArr);
		}
		
		public function updateSellerCatsList(sellerCat:SellerCat):void
		{
			var topURLLdr:TOPURLLoader = new TOPURLLoader();
			var reqParamArr:Array = new Array();
			reqParamArr=[new TopRequestParam("method","taobao.sellercats.list.update"),
			new TopRequestParam("name",sellerCat.name),
			new TopRequestParam("cid",sellerCat.cid),
			new TopRequestParam("pict_url",sellerCat.pict_url),
			new TopRequestParam("parent_cid",sellerCat.parent_cid),
			new TopRequestParam("sort_order",sellerCat.sort_order.toString())];
			topURLLdr.addEventListener(Event.COMPLETE,sellerCatsUpdateHandler);
			topURLLdr.loadData(reqParamArr);
		}
		
		private function sellerCatsGetHandler(event:Event):void
		{
			switch(event.type)
			{
				case Event.COMPLETE:
					var sellerCatsArr:Array = sellerCatsGetJsonParser(event.target.data);
					dispatchEvent(new TOPEvent(TOPEvent.TOP_DATA,sellerCatsArr));
				break;
				default:
					 dispatchEvent(event.clone());
				break;
			}
		}
		
		private function sellerCatsGetJsonParser(jsonStr:String):Array
		{
			if(TOPError.isError(jsonStr))
			{
				var topError:TOPError = TOPError.parseError(jsonStr);
				//dispatchEvent(new ErrorEvent(ErrorEvent.ERROR,false,false,
			}else
			{
				return objToSellerCatArray((JSON.decode(jsonStr) as Object).rsp);
			}
			return null;
		}
		
		private function objToSellerCatArray(obj:Object):Array
		{
			var sellerCatsArr:Array = new Array();
			for each(var sellerCatObj:Object in obj.seller_cats)
			{
				sellerCatsArr.push(SellerCatConverter.convert(sellerCatObj));
			}
			return sellerCatsArr;
		}
		
		private function sellerCatsAddHandler(event:Event):void
		{
			switch(event.type)
			{
				case Event.COMPLETE:
					var sellerCatAddResult:SellerCatAddResult = sellerCatsAddJsonParser(event.target.data);
					dispatchEvent(new TOPEvent(TOPEvent.TOP_DATA,sellerCatAddResult));
				break;
				default:
					 dispatchEvent(event.clone());
				break;
			}
		}
		
		private function sellerCatsAddJsonParser(jsonStr:String):SellerCatAddResult
		{
			if(TOPError.isError(jsonStr))
			{
				var topError:TOPError = TOPError.parseError(jsonStr);
				//dispatchEvent(new ErrorEvent(ErrorEvent.ERROR,false,false,
			}else
			{
				var sellerCatAddObj:Object = JSON.decode(jsonStr).rsp.seller_cats[0];
				var sellerCatAddResult:SellerCatAddResult = new SellerCatAddResult();
				sellerCatAddResult.cid = sellerCatAddObj.cid;
				sellerCatAddResult.created = TOPUtil.getCustomDate(sellerCatAddObj.created.toString());
				return sellerCatAddResult;
			}
			return null;
		}
		
		private function sellerCatsUpdateHandler(event:Event):void
		{
			switch(event.type)
			{
				case Event.COMPLETE:
					var sellerCatUpdateResult:SellerCatUpdateResult = sellerCatsUpdateJsonParser(event.target.data);
					dispatchEvent(new TOPEvent(TOPEvent.TOP_DATA,sellerCatUpdateResult));
				break;
				default:
					 dispatchEvent(event.clone());
				break;
			}
		}
		
		private function sellerCatsUpdateJsonParser(jsonStr:String):SellerCatUpdateResult
		{
			if(TOPError.isError(jsonStr))
			{
				var topError:TOPError = TOPError.parseError(jsonStr);
				//dispatchEvent(new ErrorEvent(ErrorEvent.ERROR,false,false,
			}else
			{
				var sellerCatUpdateObj:Object = JSON.decode(jsonStr).rsp.seller_cats[0];
				var sellerCatUpdateResult:SellerCatUpdateResult = new SellerCatUpdateResult();
				sellerCatUpdateResult.cid = sellerCatUpdateObj.cid;
				sellerCatUpdateResult.created = TOPUtil.getCustomDate(sellerCatUpdateObj.created.toString());
				return sellerCatUpdateResult;
			}
			return null;
		}
	}
}